package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.forms.AnalysisFilesForm;
import ca.ubc.icapture.genapha.forms.ExportForm;

import icapture.SQLMgr;
import icapture.beans.DB.Cohort;
import icapture.beans.DB.Phenotypes;
import icapture.beans.DB.User;
import icapture.beans.DBExportOptions;

import icapture.beans.Format;
import icapture.genapha.GenotypeToolsManager;
import icapture.genapha.exporters.DBExporter;
import icapture.genapha.exporters.FbatDBExporter;
import icapture.genapha.exporters.PlinkBinaryExporter;
import icapture.genapha.exporters.UnphasedDBExporter;

/**
 * Created by IntelliJ IDEA.
 * User: btripp
 * Date: Sep 13, 2006
 * Time: 9:38:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class ExportAction extends AnalysisFilesAction
{

    @Override
    protected void processRequest(AnalysisFilesForm form)
    {
        final ExportForm exportForm = (ExportForm) form;
        final Format format;
        GenotypeToolsManager gtm = new GenotypeToolsManager();

        if (exportForm.getBinarize().equalsIgnoreCase("binarize"))
        {
            // Add name to the result
            result.setName("Phenotype Binarization");
            SQLMgr.updateResult(result);

            final Phenotypes phenotype = SQLMgr.getPhenotype(Integer.parseInt(exportForm.getSelectedPhenotype()));
            final Cohort cohort = SQLMgr.getCohort(Integer.parseInt(exportForm.getSelectedCohort()[0]));
            format = gtm.FBAT_FORMAT;
            format.setBinary(true);
            gtm.writeBinarizedPhenotypeLinkageFileFromDB(phenotype, cohort,
                    exportForm.isRemoveMzTwins(), exportForm.isRemoveDuplicateSubjects(),
                    Double.parseDouble(exportForm.getCutValue()), exportForm.getCutType(), exportForm.getSelectedGenes(),
                    format, exportForm.getEmail(), result);
        }
        else if (exportForm.getBinarize().equalsIgnoreCase("cut"))
        {
            // Add name to the result
            result.setName("Phenotype Binarization");
            SQLMgr.updateResult(result);

            final Phenotypes phenotype = SQLMgr.getPhenotype(Integer.parseInt(exportForm.getSelectedPhenotype()));
            final Cohort cohort = SQLMgr.getCohort(Integer.parseInt(exportForm.getSelectedCohort()[0]));
            gtm.writeCutPhenotypeLinkageFileFromDB(phenotype, cohort,
                    exportForm.isRemoveMzTwins(), exportForm.isRemoveDuplicateSubjects(),
                    Double.parseDouble(exportForm.getCutValue()), exportForm.getCutType(), exportForm.getSelectedGenes(),
                    gtm.FBAT_FORMAT, exportForm.getEmail(), result);
        }
        else
        {
            // Add name to the result
            if (exportForm.getSelectedCohort().length > 1)
            {
                result.setName("Combined Binary Phenotype Linkage File");
            }
            else
            {
                result.setName("Binary Phenotype Linkage File");
            }
            SQLMgr.updateResult(result);

            // Init exporter and format
            final DBExportOptions options = new DBExportOptions();
            final DBExporter exporter;
            if (exportForm.getFormat().equalsIgnoreCase("SAGE"))
            {
                exporter = new FbatDBExporter(options);
                format = gtm.SAGE_FORMAT;
            }
            else if (exportForm.getFormat().equalsIgnoreCase("UNPHASED"))
            {
                exporter = new UnphasedDBExporter(options);
                format = gtm.UNPHASED_FORMAT;
                // GOTCHA: No need to worry about format's unaffected
                // because Busselton is not part of NHL
            }
            else if (exportForm.getFormat().equalsIgnoreCase("BPlink"))
            {
                exporter = new PlinkBinaryExporter(options);
                // GOTCHA: PlinkBinaryExporter does not use format in option
                // Thus, setting this to null
                format = null;
            }
            else
            {
                exporter = new FbatDBExporter(options);
                format = gtm.FBAT_FORMAT;
            }

            // Setup options
            options.setPhenotypeID(Integer.valueOf(exportForm.getSelectedPhenotype()));
            options.setCohortIds(exportForm.getSelectedCohort());
            options.setRemoveMZTwins(exportForm.isRemoveMzTwins());
            options.setRemoveDups(exportForm.isRemoveDuplicateSubjects());
            options.setGeneIDs(exportForm.getSelectedGenes());
            options.setEthnicityIDs(exportForm.getSelectedEthnicity());
            options.setResult(result);
            options.setFormat(format);
            options.setFilePerGene(exportForm.isFilePerGene());
            options.setCohortPopulationIDs(exportForm.getSelectedSubCohort());
            options.setZeroIncomplete(exportForm.isZeroIncomplete());
            options.setZeroControls(exportForm.isZeroControls());
            options.setHyperControls(exportForm.isHyperControls());
            options.setCaseControl(exportForm.isCaseControl());
            options.setRsNumbers(exportForm.getSelectedSnps());
            User user = new User();
            user.setEmail(exportForm.getEmail());
            options.setUser(user);
            options.setCovariates(exportForm.getSelectedCovariates());

            exporter.export();
        }
    }
}
